home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / sgwnd10 / stringhe.bas < prev    next >
Encoding:
BASIC Source File  |  1998-06-17  |  2.4 KB  |  85 lines

  1. Attribute VB_Name = "StringHelpers"
  2. Option Explicit
  3.  
  4. Public Function Str_Replace(ByRef s$, ByVal name$, ByVal val$) As Boolean
  5.     Str_Replace = False
  6.     Dim pos&
  7.     pos = InStr(s, name)
  8.     If (pos > 0) Then
  9.         Dim nameLen%
  10.         nameLen = Len(name)
  11.         s = Left(s, pos - 1) + val + Mid(s, pos + nameLen)
  12.         Str_Replace = True
  13.     End If
  14. End Function
  15.  
  16. Public Function Str_ReplaceAll(ByRef s$, ByVal name$, ByVal val$) As Boolean
  17.     Str_ReplaceAll = False
  18.     Do
  19.         Dim bReplaced As Boolean
  20.         bReplaced = Str_Replace(s, name, val)
  21.         If Not bReplaced Then Exit Do
  22.         Str_ReplaceAll = True
  23.     Loop
  24. End Function
  25.  
  26. Public Function Str_Token(ByVal s$, ByVal sep$, ByVal Item%)
  27.     Dim s0$, pos1&, pos2&
  28.     
  29.     s0 = ""
  30.     Str_Token = ""
  31.     pos2 = 0
  32.     Do While Item >= 0
  33.         s0 = ""
  34.         pos2 = InStr(s, sep)
  35.         If (pos2 = 0) And (Item <> 0) Then Exit Do
  36.         
  37.         If pos2 = 0 Then
  38.             s0 = s
  39.         Else
  40.             s0 = Left(s, pos2 - 1)
  41.         End If
  42.         s = Mid(s, pos2 + 1)
  43.         Item = Item - 1
  44.     Loop
  45.     Str_Token = s0
  46. End Function
  47.  
  48. '----------------------------------------------------------------------
  49. ' Returns copy of the specified string without leading and trailing
  50. ' whitespace characters
  51. '----------------------------------------------------------------------
  52. Public Function Str_Trim(s$) As String
  53.     Str_Trim = Str_TrimEx(s, " " & Chr(9) & Chr(10) & Chr(13))
  54. End Function
  55.  
  56. '----------------------------------------------------------------------
  57. ' Returns copy of the specified string without leading and trailing
  58. ' characters specified in sWhat$
  59. '----------------------------------------------------------------------
  60. Public Function Str_TrimEx(s$, sWhat$) As String
  61.     Dim nSize&, nFirst&, nLast&
  62.     
  63.     Str_TrimEx = ""
  64.  
  65.     ' Find first non whitespace character
  66.     nSize = Len(s)
  67.     nFirst = 1
  68.     Do
  69.         If (InStr(sWhat, Mid(s, nFirst, 1)) = 0) Then Exit Do
  70.         nFirst = nFirst + 1
  71.         If (nFirst > nSize) Then Exit Function
  72.     Loop
  73.     
  74.     ' Find last non whitespace character
  75.     nLast = nSize
  76.     Do
  77.         If (InStr(sWhat, Mid(s, nLast, 1)) = 0) Then Exit Do
  78.         nLast = nLast - 1
  79.         If (nLast < nFirst) Then Exit Function
  80.     Loop
  81.     
  82.     Str_TrimEx = Mid(s, nFirst, nLast - nFirst + 1)
  83. End Function
  84.  
  85.